<template>
  <div class="nav" :class="!sidebar ? 'nav-w' : 'nav-wl'">
    <el-menu
      default-active="2"
      class="el-menu-vertical-ny mynav"
      background-color="#0d2439"
      text-color="#fff"
      active-text-color="rgb(105,179,255)"
      :router="true"
      :collapse="sidebar"
    >
      <el-submenu
        v-for="item in routes"
        :key="item.path"
        :index="item.path"
        v-show="item.meta.type == type || type == '1'"
      >
        <template slot="title">
          <i class="iconfont" :class="item.meta.icon"></i>
          <span>{{ item.meta.title }}</span>
        </template>
        <el-menu-item
          v-for="i in item.children"
          :key="i.path"
          :index="item.path + '/' + i.path"
          >{{ i.meta.title }}</el-menu-item
        >
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
// 引入vuex工具方法
import { mapGetters } from "vuex";

export default {
  name: "sidebar",
  data() {
    return {
      routes: [],
    };
  },
  computed: {
    ...mapGetters(["sidebar", "type"]),
  },
  created() {
    // 获取路由数据，重组，保存到本地变量
    let routes = [];
    this.$router.options.routes.forEach((item) => {
      if (item.meta) {
        routes.push(item);
      }
    });
    this.routes = routes;
  },
};
</script>

<style lang="scss" scoped>
.el-menu-vertical-ny:not(.el-menu--collapse) {
  width: 176px;
  min-height: 400px;
}
.el-menu {
  border-right: none;
}

.nav {
  background-color: #0d2439;
  color: #fff;
  overflow: hidden;
}

.iconfont {
  font-size: 18px;
  padding-right: 5px;
}
</style>
